<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}

// 获取表模型
$table_er_kami = C::t('#erling_kami_member#er_kami_member');

$url_page = ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_kami_member&pmod=admin_kami_list';
// 获取当前列表
$page = max(1, intval($page));
$pagesize = max($_GET['pagesize']??20, intval($pagesize));
$start = ($page - 1) * $pagesize;

$expire_time = isset($_GET['expire_time']) ? strtotime($_GET['expire_time']) : TIMESTAMP;
$expire_date = ($expire_time != -1 && is_numeric($expire_time)) ? date('Y-m-d', $expire_time) : '';// 转换为日期格式

$query = [];
if(!empty($_GET['id'])){
    $query['id'] = intval($_GET['id']);
    $url_page .= '&id='.$_GET['id'];
}
if(!empty($_GET['uid'])){
    $query['uid'] = intval($_GET['uid']);
    $url_page .= '&uid='.$_GET['uid'];
}
if(!empty($_GET['status'])){
    $query['status'] = intval($_GET['status']);
    $url_page .= '&status='.$_GET['status'];
}
if(!empty($_GET['kami_key'])){
    $query['kami_key'] = intval($_GET['kami_key']);
    $url_page .= '&kami_key='.$_GET['kami_key'];
}
if(!empty($_GET['user_group'])){
    $query['user_group'] = intval($_GET['user_group']);
    $url_page .= '&user_group='.$_GET['user_group'];
}
if(!empty($_GET['user_group_days'])){
    $query['user_group_days'] = intval($_GET['user_group_days']);
    $url_page .= '&user_group_days='.$_GET['user_group_days'];
}
if(!empty($_GET['expire_time'])){
    $url_page .= '&expire_time='.$_GET['expire_time'];
}
$result = $table_er_kami->fetch_all($page, $pagesize, $query, $expire_time);
$kami_list = $result['data'];
$total = $result['total'];

if(!empty($_GET['pagesize'])){
    $url_page .= '&pagesize='.intval($_GET['pagesize']);
}
$multipage = multi($total, $pagesize, $page, $url_page);

// 获取用户组
$all_groups = C::t('common_usergroup')->range();
$system_groups = [];// 系统用户组
$member_groups = [];// 会员用户组
$special_groups = [];// 自定义用户组  
$admin_groups = [];// 自定义管理组    
foreach($all_groups as $group) {
    if($group['type'] == 'special') {
        if($group['radminid'] > 0) {
            $admin_groups[] = $group;
        } else {
            $special_groups[] = $group;
        }
    }
    if($group['type'] == 'system') {
        $system_groups[] = $group;
    }
    if($group['type'] == 'member') {
        $member_groups[] = $group;
    }
}

$group_map = [];// 所有用户组映射
foreach($all_groups as $group) {
    $group_map[$group['groupid']] = $group['grouptitle'];
}

include template('erling_kami_member:admin_kami_list');
